cmake_minimum_required(VERSION 3.5.1)

project(server VERSION 1.3.0)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

find_package(Threads REQUIRED)
find_package(asio REQUIRED)

include(GenerateExportHeader)

# Public API include directory
set (includes
    include/fineftp/server.h
    include/fineftp/permissions.h
)

# Private source files
set(sources
    src/filesystem.cpp
    src/filesystem.h
    src/ftp_message.h
    src/ftp_session.cpp
    src/ftp_session.h
    src/ftp_user.h
    src/server.cpp
    src/server_impl.cpp
    src/server_impl.h
    src/user_database.cpp
    src/fineftp_export.h
)


add_library (${PROJECT_NAME}
    ${includes}
    ${sources}
)

# Generate version defines
#configure_file("fineftp_version.h.in" "${PROJECT_BINARY_DIR}/fineftp_version.h" @ONLY)

# Generate header with export macros
#generate_export_header(${PROJECT_NAME}
#  EXPORT_FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR}/src/fineftp_export.h
#  BASE_NAME FINEFTP
#)

add_library (fineftp::server ALIAS ${PROJECT_NAME})


target_link_libraries(${PROJECT_NAME}
    PRIVATE
        Threads::Threads
)

# Link asio as described in this workaround: https://gitlab.kitware.com/cmake/cmake/-/issues/15415#note_633938
target_link_libraries     (${PROJECT_NAME} PRIVATE $<BUILD_INTERFACE:asio::asio>)

target_compile_definitions(${PROJECT_NAME}
    PRIVATE
        ASIO_STANDALONE
        ASIO_DISABLE_VISIBILITY
        __USE_FILE_OFFSET64=1
)

target_compile_options(${PROJECT_NAME} PRIVATE
                           $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
                                -Wall -Wextra>
                           $<$<CXX_COMPILER_ID:MSVC>:
                                /W4>)


# Add own public include directory
target_include_directories(${PROJECT_NAME}
  PUBLIC 
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>          # To find the export file generated by generate_export_header
    $<INSTALL_INTERFACE:include>
)

target_include_directories(${PROJECT_NAME} PRIVATE src)

set_target_properties(${PROJECT_NAME} PROPERTIES
    VERSION ${PROJECT_VERSION}
    SOVERSION ${PROJECT_VERSION_MAJOR}
    OUTPUT_NAME fineftp-${PROJECT_NAME}
)

##################################

# Mirror the folder structure in an IDE
include(sourcetree.cmake)
create_source_tree(${includes} ${sources})

##################################

set(FINEFTP_INSTALL_CMAKE_DIR "lib/cmake/fineftp")

include(CMakePackageConfigHelpers)

configure_package_config_file(
  "../cmake/fineftpConfig.cmake.in"
  "${CMAKE_CURRENT_BINARY_DIR}/cmake/fineftpConfig.cmake"
  INSTALL_DESTINATION ${FINEFTP_INSTALL_CMAKE_DIR}
  PATH_VARS FINEFTP_INSTALL_CMAKE_DIR
)

